package com.foreknow.springioc;

import com.foreknow.bean.User;
import com.foreknow.service.UserService;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassPathXmlApplicationContext implements BeanFactory {
    //创建一个Map容器用于保存bean
    private Map<String,Object> beans = new HashMap<>();
    //解决XML文件
    public ClassPathXmlApplicationContext() throws JDOMException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //1.创建SAXBuilder对象
        SAXBuilder saxBuilder = new SAXBuilder();
        //2.解析beans.xml文件
        Document document = saxBuilder.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml"));
        //3.获取根节点
        Element root = document.getRootElement();
        //4.获取子节点
        List<Element> list = root.getChildren();
        //5.遍历子节点
        for(int i = 0;i<list.size();i++) {
            Element element = list.get(i);
            //获取属性对应的值
            String id = element.getAttributeValue("id");
            String clazz = element.getAttributeValue("class");
            //将clazz转换为对象
            Object o = Class.forName(clazz).newInstance();
            //将id所对应的class保存到map容器中
            beans.put(id,o);
            //获取到 <property name="userDao" bean="u"></property>子节点
            for (Element propertyElement:element.getChildren()) {
                String name = propertyElement.getAttributeValue("name");
                String bean = propertyElement.getAttributeValue("bean");
                //从map集合中获取到对象
                Object beanObject = beans.get(bean);//UserDaoImpl对象
                //拼接set方法 public void setUserDao(UserDao userDao)
                String methodName = "set" + name.substring(0,1).toUpperCase() + name.substring(1); //setUserDao
                //通过反射动态调用set方法
                Method method = o.getClass().getMethod(methodName,beanObject.getClass().getInterfaces()[0]);
                method.invoke(o,beanObject);
            }
        }
    }
    @Override
    public Object getBean(String id) {
        return beans.get(id);
    }

    public static void main(String[] args) {
        try {
            //调用构造方法初始化
            BeanFactory factory = new ClassPathXmlApplicationContext();
            UserService userService = (UserService) factory.getBean("userService");
            userService.save(new User());
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
